package com.jingtai.shop.service.user.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jingtai.common.enums.DefaultStatusEnum;
import com.jingtai.common.enums.DeleteFlagEnum;
import com.jingtai.common.utils.MyBeanUtils;
import com.jingtai.core.entity.member.MemberAddress;
import com.jingtai.core.mapper.member.MemberAddressMapper;
import com.jingtai.shop.service.user.MemberAddressService;
import com.jingtai.shop.webfacade.bo.address.SaveAddressBO;
import com.jingtai.shop.webfacade.bo.address.UpdateAddressBO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * <p>
 * 用户收货地址表 服务实现类
 * </p>
 *
 * @author GR123
 * @since 2020-11-29
 */
@Service
public class MemberAddressServiceImpl extends ServiceImpl<MemberAddressMapper, MemberAddress> implements MemberAddressService {

    @Resource
    private MemberAddressService memberAddressService;

    /**
     * 查询用户地址分页列表
     */
    @Override
    public Page<MemberAddress> pageMemberAddress(Long userId, Long pageNo, Long pageSize) {
        return memberAddressService.page(new Page<>(pageNo, pageSize), Wrappers.lambdaQuery(MemberAddress.class)
                .eq(MemberAddress::getMemberId, userId)
                .eq(MemberAddress::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
    }

    /**
     * 添加用户地址
     */
    @Override
    public boolean saveMemberAddress(SaveAddressBO bo, Long userId) {

       /*
       添加用户地址,如果用户都没有设置默认值,就不设置,当用户购买商品的时候自己选择
                  如果用户设置了默认值 1,先查看拥有默认值的数据,有的话改成非默认
                                    2,没有的话,直接添加
       */
        MemberAddress memberAddress = MyBeanUtils.copyPropertiesChaining(bo, MemberAddress::new);
        memberAddress.setMemberId(userId);

        //传参:默认
        if (ObjectUtil.equal(DefaultStatusEnum.IS_DEFAULT.getStatus(), bo.getIsDefault())) {
            //先查看该用户是否存在默认设置的地址,有的话修改成不默认,没有的直接添加
            MemberAddress address = memberAddressService.getOne(Wrappers.lambdaQuery(MemberAddress.class)
                    .eq(MemberAddress::getMemberId, userId)
                    .eq(MemberAddress::getIsDefault, DefaultStatusEnum.IS_DEFAULT.getStatus())
                    .eq(MemberAddress::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
            if (ObjectUtil.isNotEmpty(address)) {
                address.setIsDefault(DefaultStatusEnum.NOT_DEFAULT.getStatus());
                memberAddressService.updateById(address);
            }
        }
        return memberAddressService.save(memberAddress);
    }

    /**
     * 设置默认地址
     * @param userId 用户id
     * @param id 地址id
     * @return true/false
     */
    @Override
    public boolean setDefaultAddress(Long userId,Long id) {

        //先查询是否存在该用户的默认地址
        MemberAddress existAddress = memberAddressService.getOne(Wrappers.lambdaQuery(MemberAddress.class)
                .eq(MemberAddress::getMemberId, userId)
                .eq(MemberAddress::getIsDefault, DefaultStatusEnum.IS_DEFAULT.getStatus())
                .eq(MemberAddress::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
        if (ObjectUtil.isNotEmpty(existAddress)){
            existAddress.setIsDefault(DefaultStatusEnum.NOT_DEFAULT.getStatus());
            memberAddressService.updateById(existAddress);
        }

        MemberAddress address = new MemberAddress();
        address.setId(id);
        address.setIsDefault(DefaultStatusEnum.IS_DEFAULT.getStatus());
        return memberAddressService.updateById(address);
    }

    /**
     * 删除用户地址
     *
     * @param id 地址主键ID
     * @return
     */
    @Override
    public boolean deleteMemberAddress(Long id) {
        MemberAddress address = new MemberAddress();
        address.setId(id);
        address.setDeleteFlag(DeleteFlagEnum.DELETE.getCode());
        return memberAddressService.updateById(address);
    }

    /**
     * 根据id查询用户地址信息
     */
    @Override
    public MemberAddress queryMemberAddress(Long id) {
        return memberAddressService.getById(id);
    }

    /**
     * 修改用户地址
     */
    @Override
    public boolean updateMemberAddress(UpdateAddressBO bo) {
        MemberAddress memberAddress = MyBeanUtils.copyPropertiesChaining(bo, MemberAddress::new);
        return memberAddressService.updateById(memberAddress);
    }

    /**
     * 根剧用户id查询默认地址
     */
    @Override
    public MemberAddress queryDefaultAddress(Long userId) {
        return memberAddressService.getOne(Wrappers.lambdaQuery(MemberAddress.class)
                .eq(MemberAddress::getMemberId, userId)
                .eq(MemberAddress::getIsDefault, DefaultStatusEnum.IS_DEFAULT.getStatus())
                .eq(MemberAddress::getDeleteFlag, DeleteFlagEnum.NOT_DELETE.getCode()));
    }
}
